﻿Imports System
Imports System.Collections.Specialized
Imports System.Workflow.ComponentModel.Compiler

Namespace System.Workflow.ComponentModel
    Friend NotInheritable Class CompensateValidator
        Inherits ActivityValidator
        ' Methods
        Public Overrides Function Validate(ByVal manager As ValidationManager, ByVal obj As Object) As ValidationErrorCollection
            Dim errors As ValidationErrorCollection = MyBase.Validate(manager, obj)
            Dim compensate As CompensateActivity = [TryCast](obj,CompensateActivity)
            If (compensate Is Nothing) Then
                Throw New ArgumentException(SR.GetString("Error_UnexpectedArgumentType", New Object() { GetType(CompensateActivity).FullName }), "obj")
            End If
            Dim parent As CompositeActivity = compensate.Parent
            Do While (Not parent Is Nothing)
                If ((TypeOf parent Is CompensationHandlerActivity OrElse TypeOf parent Is FaultHandlerActivity) OrElse TypeOf parent Is CancellationHandlerActivity) Then
                    Exit Do
                End If
                parent = parent.Parent
            Loop
            If (parent Is Nothing) Then
                errors.Add(New ValidationError(SR.GetString("Error_CompensateBadNesting"), &H509))
            End If
            Dim item As ValidationError = Nothing
            Dim compensatableTargets As StringCollection = CompensateActivity.GetCompensatableTargets(compensate)
            If String.IsNullOrEmpty(compensate.TargetActivityName) Then
                item = ValidationError.GetNotSetValidationError("TargetActivityName")
            ElseIf Not compensatableTargets.Contains(compensate.TargetActivityName) Then
                item = New ValidationError(SR.GetString("Error_CompensateBadTargetTX", New Object() { "TargetActivityName", compensate.TargetActivityName, compensate.QualifiedName }), &H563, False, "TargetActivityName")
            End If
            If (Not item Is Nothing) Then
                errors.Add(item)
            End If
            Return errors
        End Function

    End Class
End Namespace

